/**
 * 
 */
$(document).ready(function() {
	$("form").submit(function() {
		return false;
	});

	var VAL = {
		EVT_USR : $("#userId").text(),
		T_FDPAPLYWO : "FDPAPLYWO",
		T_FDPAPLYSO : "FDPAPLYSO",
		T_FBPMDLDEF : "FBPMDLDEF",
		T_FBPBISMTR : "FBPBISMTR",
		T_FBPBISCUS : "FBPBISCUS",
		T_FBPBISBOM : "FBPBISBOM",
        T_FBPBISPTH : "FBPBISPTH",
        T_FBPBISDAT : "FBPBISDAT",
		NORMAL : "0000000",
		DISABLED_ATTR : {
			"disabled" : true
		},
		ENABLED_ATTR : {
			"disabled" : false
		}
	};

	var domObj = {
		W : $(window),
		$cusIdFk : $("#cusIdFk"),
		$mdlIdFk : $("#mdlIdFk"),
		$woCate : $("#woCate"),
        $pathVerFk : $("#pathVerFk"),

		$woId : $("#woId"),
		$plnPrdQty : $("#plnPrdQty"),

		$plnStbDatepicker : $("#plnStbDatepicker"),
		$plnCompDatepicker : $("#plnCompDatepicker"),
		$woNote : $("#woNote"),
		$woDsc : $("#woDsc"),
		buttons : {
			$query_btn : $("#query_btn"),
			$save_btn : $("#save_btn"),
			$update_btn : $("#update_btn"),
			$del_btn : $("#del_btn"),
			$clear_btn : $("#clear_btn"),
			$bind_btn : $("#bind_btn"),
            $bind_mtrl_btn : $("#bind_mtrl_btn")
		},
        dialog : {
            $bindMtrlDialog : $("#bindMtrlDialog"),
            $mtrlCateSel    : $("#mtrlCateSel"),

            $woMtrlDiv : $("#woMtrlDiv"),
            $woMtrlGrd : $("#woMtrlGrd"),
            $woMtrlPg : $("#woMtrlPg"),

            $bindMtrlDialog_sureBtn : $("#bindMtrlDialog_sureBtn"),
		},
		grids : {
			$innerOrderGrd : $("#innerOrderGrd"),
			$cusOrderGrd : $("#cusOrderGrd"),
	        $UnderDiv         : $(".UnderDiv"),
	        $innerOrderDiv : $("#innerOrderDiv")
		}
	};

	function initInnerOrder() {
		domObj.grids.$innerOrderGrd.jqGrid({
			url : "",
			datatype : "local",
			mtype : "POST",
			autowidth : true,
            shrinkToFit:false,
            scroll: true,
			resizable : true,
			loadonce : true,
			viewrecords : true,
			rownumbers : true,
			emptyrecords : true,
			colModel : [ {
				name : 'crt_timestamp',
				index : 'crt_timestamp',
				label : CRT_TIMESTAMP,
				width : 170
			},{
                name : 'so_id',
                index : 'so_id',
                label : SO_ID_TAG,
                width : 110
            }, {
				name : 'wo_id',
				index : 'wo_id',
				label : WO_ID_TAG,
				width : 110
			}, {
				name : 'wo_stat',
				index : 'wo_stat',
				label : WO_STAT_TAG,
				width : 70
			}, {
				name : 'cus_id_fk',
				index : 'cus_id_fk',
				label : CUS_ID_TAG,
				width : 110
			}, {
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : MDL_ID_TAG,
				width : 130
			},  {
				name : 'path_id_fk',
				index : 'path_id_fk',
				label : PATH_ID_TAG,
				width : 130
			},{
				name : 'pln_prd_qty',
				index : 'pln_prd_qty',
				label : PLN_PRD_QTY_TAG,
				width : 110
			}, {
                name : 'rl_prd_qty',
                index : 'rl_prd_qty',
                label : RL_PRD_QTY_TAG,
                width : 110
            },{
                name  : 'comp_prd_qty',
                index : 'comp_prd_qty',
                label : COMP_PRD_QTY_TAG,
                width : 100
            },{
                name : 'wh_in_prd_qty',
                index : 'wh_in_prd_qty',
                label : WH_IN_PRD_QTY_TAG,
                width : 100
            },{
                name : 'scrp_prd_qty',
                index : 'scrp_prd_qty',
                label : SCRP_PRD_QTY_TAG,
                width : 100
            }, {
				name : 'pln_stb_timestamp',
				index : 'pln_stb_timestamp',
				label : PLN_STB_TIMESTAMP_TAG,
				width : 110
			}, {
				name : 'pln_cmp_timestamp',
				index : 'pln_cmp_timestamp',
				label : PLN_CMP_TIMESTAMP_TAG,
				width : 110
			}, {
                name : 'wo_dsc',
                index : 'wo_dsc',
                label : WO_DSC_TAG,
                width : 100
            }, {
                name : 'wo_note',
                index : 'wo_note',
                label : REMARK_TAG,
                width : 100
            }],
			pager : "innerOrderPg"
		});
	};

	function initCusOrder() {
		domObj.grids.$cusOrderGrd.jqGrid({
			url : "",
			datatype : "local",
			mtype : "POST",
            width : $("#bindDialog").width() * 0.73,
            autowidth:true,
            shrinkToFit:true,
            scroll: true,
			resizable : true,
			loadonce : true,
			viewrecords : true,
			rownumbers : true,
			emptyrecords : true,
			colModel : [ {
				name : 'crt_timestamp',
				index : 'crt_timestamp',
				label : CRT_TIMESTAMP,
				width : 140
			}, {
                name : 'so_id',
                index : 'so_id',
                label : CUS_SO_ID_TAG,
                width : 140
            }, {
				name : 'wo_id',
				index : 'wo_id',
				label : SO_ID_TAG,
				width : 140
			}, {
				name : 'cus_id_fk',
				index : 'cus_id_fk',
				label : CUS_ID_TAG,
				width : 100
			}, {
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : MDL_ID_TAG,
				width : 140
			}, {
				name : 'pln_prd_qty',
				index : 'pln_prd_qty',
				label : PLN_PRD_QTY_TAG,
				width : 90
			}, {
				name : 'pln_stb_timestamp',
				index : 'pln_stb_timestamp',
				label : PLN_STB_TIMESTAMP_TAG,
				width : 140
			}, {
				name : 'pln_cmp_timestamp',
				index : 'pln_cmp_timestamp',
				label : PLN_CMP_TIMESTAMP_TAG,
				width : 140
			}],
			pager : "cusOrderPg"
		});
        domObj.dialog.$woMtrlGrd.jqGrid({
            url : "",
            datatype : "local",
            mtype : "POST",
            autowidth : true,
            shrinkToFit:false,
            scroll: true,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            emptyrecords : true,
			multiselect : true,
            cellEdit:true,
            cellsubmit:"clientArray",
            colModel : [ {
                name : 'mtrl_prod_id',
                index : 'mtrl_prod_id',
                label : MTRL_ID_TAG,
                width : 160
            }, {
                name : 'mtrl_prod_dsc',
                index : 'mtrl_prod_dsc',
                label : MTRL_PROD_DSC_TAG,
                width : 150
            }, {
                name : 'mtrl_cate',
                index : 'mtrl_cate',
                label : MTRL_CATE_TAG,
                width : 150
            },{
                name : 'mtrl_use_qty',
                index : 'mtrl_use_qty',
                label : MTRL_USE_QTY_TAG,
                editable:true,
                width : 150
            }],
            pager : "woMtrlPg",
            loadComplete:function(){
            	//获取工单号，和物料类型，查询工单bom，显示已经绑定的物料信息
                var selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
                var rowData  = domObj.grids.$innerOrderGrd.jqGrid("getRowData",selRowId);
                var woId = rowData.wo_id;
                var mtrl_cate = $.trim(domObj.dialog.$mtrlCateSel.val());
                if(!mtrl_cate){
                    return false;
				}
                iaryB = {
                    wo_id : woId,
                    mtrl_cate :mtrl_cate
                };
                inObj = {
                    trx_id : VAL.T_FDPAPLYWO,
                    action_flg : "T",
                    iaryB : [ iaryB ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
					var oaryC = outObj.oaryC;
                    var ids = domObj.dialog.$woMtrlGrd.jqGrid('getDataIDs');
                   // var data =  domObj.dialog.$woMtrlGrd.jqGrid("getRowData");
                    for(var i=0;i<ids.length;i++){
                    	var rowData = domObj.dialog.$woMtrlGrd.jqGrid('getRowData',ids[i]);
                    	for(var j=0;j<oaryC.length;j++){
                    		if(rowData.mtrl_prod_id==oaryC[j].mtrl_prod_id){
                                domObj.dialog.$woMtrlGrd.jqGrid('setSelection',ids[i]);
                                domObj.dialog.$woMtrlGrd.jqGrid("setCell",ids[i],"mtrl_use_qty",oaryC[j].mtrl_use_qty);
							}
						}
                    }
                }
			}
		});

	};
	// 初始化下拉框
	var initSelectFnc = {
		// 1.初始化订单类型
		initOrderType : function() {
			comAddValueByDataCateFnc("#woCate", "WOCT", "data_ext","data_desc",true);
		},
	
		// 2.初始化 客户代码
		iniCusIdSel : function(cusIdObj) {
			var inObj, outObj, tblCnt, warp, option;
			var iary = {
				cus_typ : 'R'
			};
			inObj = {
				trx_id : VAL.T_FBPBISCUS,
				action_flg : 'Q',
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
	            SelectDom .addSelectArr(domObj.$cusIdFk, outObj.oary, "cus_id","cus_id","",true);
			}
		},
	
		// 4.产品代码 
		initmdlId : function() {
			var inObj, outObj, i, tblCnt, oary, warp, option;
            var iary = {
            		valid_flg :"Y"
            }

			inObj = {
				trx_id : VAL.T_FBPMDLDEF,
				action_flg : "Q",
                iary : [ iary ]
			};
	
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.mdl_id;
                    var val2 = obj2.mdl_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
	            SelectDom .addSelectArr(domObj.$mdlIdFk, outObj.oary, "mdl_id","mdl_id","",true);
			}
		},
	
		// 5.来料日期 //6.交货日期
		iniDateTimePicker : function() {
			var datepickerData;
			domObj.$plnStbDatepicker.datetimepicker({
				language : 'zh-CN',
				autoclose : true,
				pickTime : false
			});
			domObj.$plnStbDatepicker.datetimepicker().on(
					'changeDate',
					function(ev) {
						domObj.$plnStbDatepicker
								.datetimepicker('hide');
					});
			domObj.$plnCompDatepicker.datetimepicker({
				language : 'zh-CN',
				autoclose : true,
				pickTime : false
			});
			domObj.$plnCompDatepicker.datetimepicker().on('changeDate',
					function(ev) {
						domObj.$plnCompDatepicker
								.datetimepicker('hide');
					});
			datepickerData = domObj.$plnStbDatepicker.data("datetimepicker");
			datepickerData.setLocalDate(datepickerData.getLocalDate());
			datepickerData = domObj.$plnCompDatepicker.data("datetimepicker");
			datepickerData.setLocalDate(datepickerData.getLocalDate());
		},
        iniMtrlCate : function(){
            var inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg : "Q",
                iary : [{
                    data_cate : "MTCT",
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var options = [];
                domObj.dialog.$mtrlCateSel.append("<option ></option>");
    			for(var i=0; i<outObj.oary.length; i++){
    				if(outObj.oary[i]["data_ext"] != "BACK"){//除去备品备件类型的物料
        				options[i] = "<option value ="+ outObj.oary[i]["data_ext"] +">" + outObj.oary[i]["data_desc"] + "</option>";
    				}
    			}
    			domObj.dialog.$mtrlCateSel.append(options.join(""));
                domObj.dialog.$mtrlCateSel.select2({
                	theme : "bootstrap",
                	width : 200
                });
            }
        }
	};

	function init() {
		initSelectFnc.initOrderType();
		initSelectFnc.iniCusIdSel();
		initSelectFnc.initmdlId();
		initSelectFnc.iniDateTimePicker();
        initSelectFnc.iniMtrlCate();
	};
	
	var baseFnc = {
		convertTimestamp2Date : function(timestamp) {
			var date = new Date(timestamp.substring(0, 10));
			/*
			 * date.setHours(timestamp.substring(11, 13));
			 * date.setMinutes(timestamp.substring(14, 16));
			 * date.setSeconds(timestamp.substring(17, 19));
			 */
			return date;
		}
	};
	
	var buttonFnc = {
		queryFnc : function() {
			var woId, woCate, cusIdFk, mdlIdFk, iary, inObj, outObj;
			woId = $.trim(domObj.$woId.val());
			woCate = $.trim(domObj.$woCate.val());
			cusIdFk = $.trim(domObj.$cusIdFk.val());
			mdlIdFk = $.trim(domObj.$mdlIdFk.val());
			iary = {
				wo_id : woId,
				wo_cate : woCate,
				cus_id_fk : cusIdFk,
				mdl_id_fk : mdlIdFk
			};
			inObj = {
				trx_id : VAL.T_FDPAPLYWO,
				action_flg : "Q",
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#innerOrderGrd");
			}
		},

		saveFnc : function() {
			var actionFlg, inObj, outObj, iary, newRowId, rowIds, selRowId;
			var woId, woCate, cusIdFk, mdlIdFk, plnPrdQty, woNote;
			var plnStbTimestamp, plnCompTimestamp, woDsc;
			
			woId = $.trim(domObj.$woId.val());
			woCate = $.trim(domObj.$woCate.val());
			cusIdFk = $.trim(domObj.$cusIdFk.val());
			mdlIdFk = $.trim(domObj.$mdlIdFk.val());
			plnPrdQty = $.trim(domObj.$plnPrdQty.val());
			woNote = $.trim(domObj.$woNote.val());
			woDsc = $.trim(domObj.$woDsc.val());
			plnStbTimestamp = domObj.$plnStbDatepicker.data("datetimepicker").getLocalDate().format('yyyy-MM-dd');
			plnCompTimestamp = domObj.$plnCompDatepicker.data("datetimepicker").getLocalDate().format('yyyy-MM-dd');

			// 可编辑说明为新增，不可编辑说明为修改
			actionFlg = domObj.$woId.attr("disabled") === "disabled" ? "U" : "A";

			if (!woCate) {
				showErrorDialog("", "工单类型不能为空,请选择!");
				return false;
			}
			if (!woId) {
				showErrorDialog("", "工单代码不能为空,请输入!");
				return false;
			}
			if (!plnPrdQty) {
				showErrorDialog("", "计划投产数量不能为空,请输入!");
				return false;
			} else if (!ComRegex.isInt(plnPrdQty)){
                showErrorDialog("","计划投产数量不是有效的整数");
                return false;
            }
			if (!cusIdFk) {
				showErrorDialog("", "客户代码不能为空,请选择!");
				return false;
			}
			if (!plnStbTimestamp) {
				showErrorDialog("", "请选择计划投产日期!");
				return false;
			}
			if (!plnCompTimestamp) {
				showErrorDialog("", "请选择计划完成日期!");
				return false;
			}
			if (baseFnc.convertTimestamp2Date(plnStbTimestamp) > baseFnc.convertTimestamp2Date(plnCompTimestamp)) {
				showErrorDialog("", "计划投产时间日期不能在计划完成日期之后!");
				return false;
			}
			if (!mdlIdFk) {
				showErrorDialog("", "产品代码不能为空,请选择!");
				return false;
			}
			if (!woDsc) {
				showErrorDialog("", "工单描述不能为空!");
				return false;
			}
			// 输给后台的数据
			var iaryA =[];
			var iary={
                wo_id : woId,
                wo_cate : woCate,
                cus_id_fk : cusIdFk,
                mdl_id_fk : mdlIdFk,
                pln_prd_qty : plnPrdQty,
                pln_stb_date : plnStbTimestamp,
                pln_cmp_date : plnCompTimestamp,
                wo_note : woNote,
                wo_dsc : woDsc,
			}
            iaryA.push(iary);
			inObj = {
				trx_id       : VAL.T_FDPAPLYWO,
				action_flg  : 'A',
				opi_ope_flg : 'Y',
				evt_usr      :VAL.EVT_USR,
				iary         :iaryA
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				if (actionFlg == "A") {
					newRowId = getGridNewRowID("#innerOrderGrd");
					domObj.grids.$innerOrderGrd.jqGrid("addRowData", newRowId,outObj.oary[0]);
					domObj.grids.$innerOrderGrd.setSelection(newRowId, true); 
					showSuccessDialog("工单新建成功");
					$("#update_btn").attr(VAL.ENABLED_ATTR);
				} else if (actionFlg == "U") {
					selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
					domObj.grids.$innerOrderGrd.jqGrid("setRowData", selRowId,outObj.oary[0]);
					showSuccessDialog("工单信息更新成功");
				}
			}
		},

		updateFnc : function() {
			var selRowId, rowData, iary, inObj, outObj, woStat;
			
			$("input").val("");
			$("textarea").val("");
            SelectDom.setSelect(domObj.$woCate, "");
            SelectDom.setSelect(domObj.$cusIdFk,"");
            SelectDom.setSelect(domObj.$mdlIdFk,"");
            /*SelectDom.setSelect(domObj.$factorySite,"");
            SelectDom.setSelect(domObj.$factoryId,"");*/
			selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的工单");
				return false;
			}
			// 这里需要判断 下线状态
			rowData = domObj.grids.$innerOrderGrd.jqGrid("getRowData", selRowId);
			iary = {
				wo_id : rowData.wo_id,
			};
			inObj = {
				trx_id : VAL.T_FDPAPLYWO,
				action_flg : "Q",
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL && outObj.tbl_cnt>0) {
				var oary = outObj.oary[0];
				//检查产品代码是否存在
                var iary = {
                    mdl_id : oary.mdl_id_fk
                }
                var inObj_mdl = {
                    trx_id : VAL.T_FBPMDLDEF,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                var outObj_mdl = comTrxSubSendPostJson(inObj_mdl);
                if(outObj_mdl.rtn_code == VAL.NORMAL && outObj_mdl.tbl_cnt>0){
					;
				}else {
                    showErrorDialog("", "产品代码[" + oary.mdl_id_fk + "]在系统中不存在，请检查！");
                    return false;
                }
                
                //检查客户代码是否存在
                var iary = {
                    cus_id_fk : oary.cus_id_fk
                }
                var inObj_cus = {
                    trx_id : VAL.T_FBPBISCUS,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                var outObj_cus = comTrxSubSendPostJson(inObj_cus);
                if(outObj_cus.rtn_code == VAL.NORMAL && outObj_cus.tbl_cnt>0){
                    ;
                }else {
                    showErrorDialog("", "客户代码[" + oary.cus_id_fk + "]在系统中不存在，请检查！");
                    return false;
                }
                
                woStat = oary.wo_stat;
				if (woStat == "RLSE") {
					unableFn();
					domObj.$plnPrdQty.attr(VAL.ENABLED_ATTR);
                    domObj.$woNote.attr(VAL.ENABLED_ATTR);
                    domObj.$woDsc.attr(VAL.ENABLED_ATTR);
				} else {
					enableFn();
					domObj.$woId.attr(VAL.DISABLED_ATTR);
				}
				
				domObj.$woId.val(oary.wo_id);
				domObj.$plnPrdQty.val(oary.pln_prd_qty);
				domObj.$woNote.val(oary.wo_note);
				domObj.$woDsc.val(oary.wo_dsc);
				$("#plnStbDatepicker input").val(oary.pln_stb_timestamp);
				$("#plnCompDatepicker input").val(oary.pln_cmp_timestamp);
				SelectDom.setSelect(domObj.$woCate,oary.wo_cate);
				SelectDom.setSelect(domObj.$cusIdFk,oary.cus_id_fk);
				SelectDom.setSelect(domObj.$mdlIdFk,oary.mdl_id_fk);
				//设置工艺路线和工艺版本
//				SelectDom.setSelect(domObj.$pathIdFk,oary.path_id_fk);
				/*SelectDom.setSelect(domObj.$factorySite,oary.fab_site_fk);
				SelectDom.setSelect(domObj.$factoryId,oary.fab_id_fk);*/
				/*SelectDom.setSelect(domObj.$pathVerFk,oary.path_ver_fk);*/
			}
		},

		delFnc : function() {
			var selRowId, rowData, iary, inObj, outObj;
			// 获得选中的行ID
			selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要删除的工单");
				return false;
			}
			// 删除按钮绑定删除事件
			$("#del_btn").showCallBackWarnningDialog({
				errMsg : "是否删除该工单信息,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
						// 获得选中行的数据
						rowData = domObj.grids.$innerOrderGrd.jqGrid("getRowData",selRowId);
						iary = {
							wo_id : rowData.wo_id,
						};
						inObj = {
							trx_id : VAL.T_FDPAPLYWO,
							action_flg : "D",
							iary : [ iary ]
						};
						outObj = comTrxSubSendPostJson(inObj);
						if (outObj.rtn_code === VAL.NORMAL) {
							showSuccessDialog("删除工单成功");
							domObj.grids.$innerOrderGrd.jqGrid("delRowData",selRowId);
						}
					}
				}
			});
		},

/*		releaseFnc : function() {
			var selRowId, rowData, iary, inObj, outObj, woId;
			selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要下线的工单");
				return false;
			}
			rowData = domObj.grids.$innerOrderGrd.jqGrid("getRowData", selRowId);
			woId = rowData.wo_id;
			iary = {
				wo_id : woId,
			};
			inObj = {
				trx_id : VAL.T_FDPAPLYWO,
				action_flg : "R",
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				unableFn();
                domObj.$plnPrdQty.attr(VAL.ENABLED_ATTR);
				selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
				domObj.grids.$innerOrderGrd.jqGrid("setRowData", selRowId, outObj.oary[0]);
				showSuccessDialog("工单下线成功");
			}

		},*/

		clearFnc : function() {
			enableFn();
			SelectDom.initWithSpace(domObj.$cusIdFk);
			SelectDom.initWithSpace(domObj.$mdlIdFk);
			SelectDom.initWithSpace(domObj.$woCate);
			domObj.$woId.val("");
			domObj.$plnPrdQty.val("");
			domObj.$woNote.val("");
			domObj.$woDsc.val("");
			init();
			$("#innerOrderGrd").jqGrid('clearGridData');
		},

		bindFnc : function() {
			var selRowId, woId, mdlIdFk, woCate, cusIdFk, iary, inObj, outObj;
			selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要绑定的工单");
				return false;
			}
			mdlIdFk = domObj.$mdlIdFk.val();
			woCate = domObj.$woCate.val();
			cusIdFk = domObj.$cusIdFk.val();
			// 根据查询条件+S查询客户订单
			iary = {
				mdl_id_fk : mdlIdFk,
				wo_cate : woCate,
				cus_id_fk : cusIdFk,
			};
			inObj = {
				trx_id : VAL.T_FDPAPLYSO,
				action_flg : "Q",
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#cusOrderGrd");
			}

			$('#bindDialog').modal({
				backdrop : true,
				keyboard : false,
				show : false
			});

			$('#bindDialog').unbind('shown.bs.modal');
			$("#bindDialog_queryBtn").unbind('click');
			$('#bindDialog').bind('shown.bs.modal');
			$('#bindDialog').modal("show");

			// 模态框查询按钮绑定查询函数
			$("#bindDialog_queryBtn").bind('click', bindDialog.dialogBindFnc);
			$("#bindDialog input").attr(VAL.ENABLED_ATTR);
			$('#bindDialog input').val("");
		},
        bindMtrlFnc : function() {
            domObj.dialog.$woMtrlGrd.jqGrid("clearGridData");
            SelectDom.setSelect(domObj.dialog.$mtrlCateSel, "", "");
            var selRowId = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
            if (!selRowId) {
                showErrorDialog("", "请选择需要绑定的工单");
                return false;
            }
            domObj.dialog.$bindMtrlDialog.modal({
                backdrop : true,
                keyboard : false,
                show : false
            });
            domObj.dialog.$bindMtrlDialog.modal("show");
		}
	};

	var bindDialog = {
		dialogBindFnc : function() {
			var selRowId, rowData, selRowId2, rowData2, iary, inObj, outObj;
			var woId, wo_id, cus_id_fk, mdl_id_fk;
			selRowId = domObj.grids.$cusOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要绑定的客户订单");
				return false;
			}
			$("#bindDialog_queryBtn").showCallBackWarnningDialog({
				errMsg : "是否绑定到该客户订单,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
						rowData = domObj.grids.$cusOrderGrd.jqGrid("getRowData",
										selRowId);
						selRowId2 = domObj.grids.$innerOrderGrd
								.jqGrid(
										"getGridParam",
										"selrow");
						rowData2 = domObj.grids.$innerOrderGrd
								.jqGrid(
										"getRowData",
										selRowId2);
						// 内部订单的ID
						woId = rowData2.wo_id;
						//客户订单的ID 
						wo_id = rowData.wo_id;
						iary = {
							wo_id : woId,
							wo_id2 : wo_id
						};
						inObj = {
							trx_id : VAL.T_FDPAPLYWO,
							action_flg : "B",
							iary : [ iary ],
							evt_usr : VAL.EVT_USR
						};
						outObj = comTrxSubSendPostJson(inObj);
						if (outObj.rtn_code === VAL.NORMAL) {
							showSuccessDialog("成功绑定客户订单");
							$('#bindDialog').modal("hide");
							domObj.grids.$innerOrderGrd.jqGrid("setRowData",selRowId2,outObj.oary[0]);
						}
					}
				}
			});
		},
         bindMtrlSureFnc: function() {
             var idWo = domObj.grids.$innerOrderGrd.jqGrid("getGridParam", "selrow");
             if (!idWo) {
                 showErrorDialog("", "请选择需要绑定物料的工单");
                 return false;
             }
             var woData = domObj.grids.$innerOrderGrd.jqGrid("getRowData", idWo);
             var idMtrl = domObj.dialog.$woMtrlGrd.jqGrid('getGridParam', 'selarrrow');
             /*if (idMtrl.length <= 0) {
                 showErrorDialog("", "请选择需要绑定的物料");
                 return false;
             }*/
             var iaryB = []
             for (var i = 0; i < idMtrl.length; i++) {
                 if ($('#woMtrlGrd :text[role="textbox"]').length > 0) {
                     var v = $('#woMtrlGrd :text[role="textbox"]').val();
                     $('#woMtrlGrd :text[role="textbox"]').parent().attr('title', v);
                     $('#woMtrlGrd :text[role="textbox"]').parent().html(v);
                 }
                 var mtrlData = domObj.dialog.$woMtrlGrd.jqGrid("getRowData", idMtrl[i]);
                 if (!mtrlData.mtrl_use_qty) {
                     showErrorDialog("", "第'" + $('#' + idMtrl[i])[0].rowIndex + "'行单位物料使用量不能为空");
                     return false;
                 }
                 if (!ComRegex.isNumber(mtrlData.mtrl_use_qty)) {
                     showErrorDialog("", "第" + $('#' + idMtrl[i])[0].rowIndex + "行单位物料使用量数值格式不正确！");
                     return false;
                 }
                 var iary = {
                     mtrl_prod_id: mtrlData.mtrl_prod_id,
                     mtrl_prod_dsc: mtrlData.mtrl_prod_dsc,
                     mtrl_cate: mtrlData.mtrl_cate,
                     mtrl_use_qty: mtrlData.mtrl_use_qty
                 }
                 iaryB.push(iary);
             };
             inObj = {
             	 wo_id  : woData.wo_id,
                 mtrl_cate :$.trim(domObj.dialog.$mtrlCateSel.val()),
                 trx_id : VAL.T_FDPAPLYWO,
                 evt_usr : VAL.EVT_USR,
                 action_flg : "F",
                 iaryB : iaryB
             };
             outObj = comTrxSubSendPostJson(inObj);
             if (outObj.rtn_code === VAL.NORMAL) {
                 showSuccessDialog("工单绑定物料成功");
                 $('#bindMtrlDialog').modal("hide");
             }
         }
	};

	//工艺路线带出工艺版本
    var iniButtonAction = function(){
        domObj.buttons.$query_btn.bind('click',buttonFnc.queryFnc);
        domObj.buttons.$save_btn.bind('click',buttonFnc.saveFnc);
        domObj.buttons.$update_btn.bind('click',buttonFnc.updateFnc);
        domObj.buttons.$del_btn.bind('click', buttonFnc.delFnc);
//		domObj.buttons.$release_btn.bind('click',buttonFnc.releaseFnc);
        domObj.buttons.$clear_btn.bind('click',buttonFnc.clearFnc);
        domObj.buttons.$bind_btn.bind('click',buttonFnc.bindFnc);
        domObj.buttons.$bind_mtrl_btn.bind('click',buttonFnc.bindMtrlFnc);
        domObj.dialog.$bindMtrlDialog_sureBtn.bind('click',bindDialog.bindMtrlSureFnc);
    };
    var otherActionBind = function(){
		domObj.dialog.$mtrlCateSel.change(function(){
           var mtrlCate = $.trim(domObj.dialog.$mtrlCateSel.val());
           if(!mtrlCate){
           	 return false;
		   }
            var iary ={
                mtrl_cate : mtrlCate
            };
            var inObj = {
                trx_id : VAL.T_FBPBISMTR,
                action_flg : "Q",
                iary : [ iary ]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code === VAL.NORMAL){
                setGridInfo(outObj.oary, "#woMtrlGrd");
			}
        });
	}
    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight,grdDivWidth;
        grdDivWidth = domObj.W.width() - 60;
        grdDivHeight = domObj.W.height() - domObj.grids.$UnderDiv.offset().top;

        domObj.grids.$UnderDiv.height(grdDivHeight);
        domObj.grids.$innerOrderDiv.width(grdDivWidth);
        domObj.grids.$innerOrderDiv.height(grdDivHeight*0.99 - 51);
        domObj.grids.$innerOrderGrd.setGridWidth(grdDivWidth*0.99);
        domObj.grids.$innerOrderGrd.setGridHeight(grdDivHeight*0.95 - 101);
    };
    domObj.W.resize(function() {
        resizeFnc();
    });
    //将控件变成不可修改
    function unableFn() {
        domObj.$woCate.attr(VAL.DISABLED_ATTR);
        domObj.$cusIdFk.attr(VAL.DISABLED_ATTR);
        domObj.$mdlIdFk.attr(VAL.DISABLED_ATTR);
        domObj.$woId.attr(VAL.DISABLED_ATTR);
        domObj.$plnPrdQty.attr(VAL.DISABLED_ATTR);
        domObj.$woNote.attr(VAL.DISABLED_ATTR);
        domObj.$woDsc.attr(VAL.DISABLED_ATTR);
    }

    //将控件变成都可修改
    function enableFn() {
        domObj.$woCate.attr(VAL.ENABLED_ATTR);
        domObj.$cusIdFk.attr(VAL.ENABLED_ATTR);
        domObj.$mdlIdFk.attr(VAL.ENABLED_ATTR);
        domObj.$woId.attr(VAL.ENABLED_ATTR);
        domObj.$plnPrdQty.attr(VAL.ENABLED_ATTR);
        domObj.$woNote.attr(VAL.ENABLED_ATTR);
        domObj.$woDsc.attr(VAL.ENABLED_ATTR);
    }

	//页面初始化函数
	function pageInit() {
		initInnerOrder();
		initCusOrder();
		resizeFnc();
		domObj.$woId.val("");
		domObj.$plnPrdQty.val("");
		domObj.$woDsc.val("");
		domObj.$woNote.val("");
		init();
		enableFn();
        iniButtonAction();
        otherActionBind();
	}
	pageInit();

});
